package com.jf.cloud.seckill.listener;

import com.jf.cloud.api.seckill.dto.SecKillRefundDTO;
import com.jf.cloud.common.order.bo.RefundReductionStockBO;
import com.jf.cloud.common.rocketmq.config.RocketMqConstant;
import com.jf.cloud.seckill.service.SeckillSkuService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = RocketMqConstant.SECKILL_ORDER_REFUND_TOPIC, consumerGroup = RocketMqConstant.SECKILL_ORDER_REFUND_TOPIC)
public class SecKillOrderRefundConsumer implements RocketMQListener<SecKillRefundDTO> {

    @Autowired
    private SeckillSkuService seckillSkuService;

    @Override
    public void onMessage(SecKillRefundDTO secKillRefundDTO) {
        // 还原秒杀库存
        for (RefundReductionStockBO stockBO : secKillRefundDTO.getRefundReductionStocks()) {
            seckillSkuService.rollbackSecKillStock(secKillRefundDTO.getActivityId(), stockBO.getSkuId(), stockBO.getCount());
        }
    }
}
